package satk.database;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import satk.beans.interfaces.IntControlManagerDB;
import satk.objetos.Cinta;

/**
 * <h3>ManagerCinta.java</h3>
 * <div><em>Fecha de creación: 15/05/2011, 02:57:40 PM.</em></div>
 * <p></p>
 * @author Sergio Flores Genis <sf.cancun@gmail.com>
 */
public class ManagerCinta extends DBManager 
implements IntControlManagerDB<Cinta> {

    private boolean externalCnn = false;

    public ManagerCinta(Connection c) {
        super(c);
        this.externalCnn = true;
    }

    public ManagerCinta() {
        super();
    }

    @Override
    public Cinta getById(long id) {
        String query = "SELECT * FROM c_cinta WHERE (idc_cinta = ?);";
        Prepare parametros = new Prepare(1);
        parametros.add(id, Types.VARCHAR);
        Cinta categoria = null;
        try {
            ResultSet r = this.select(query, parametros);
            r.first();
            categoria = makeObject(r);
        } catch (SQLException ex) {
            Logger.getLogger(ManagerCategoria.class.getName()).log(Level.SEVERE,
                    "Error al buscar la categoria", ex);
        } finally {
            if (!externalCnn) {
                closeConexion();
            }
        }
        return categoria;
    }

    public Cinta getByColor(String name) {
        String query = "SELECT * FROM c_cinta WHERE (colc_cinta = ?);";
        Prepare parametros = new Prepare(1);
        parametros.add(name, Types.VARCHAR);
        Cinta categoria = null;
        try {
            ResultSet r = this.select(query, parametros);
            r.first();
            categoria = makeObject(r);
        } catch (SQLException ex) {
            Logger.getLogger(ManagerCategoria.class.getName()).log(Level.SEVERE,
                    "Error al buscar la categoria", ex);
        } finally {
            if (!externalCnn) {
                closeConexion();
            }
        }
        return categoria;
    }

    public List<Cinta> findCinta(String name, int limit) {
        List<Cinta> list = new LinkedList<Cinta>();
        try {
            ResultSet r = find("c_cinta", "colc_cinta", name, limit);
            while (r.next()) {
                Cinta c = makeObject(r);
                list.add(c);
            }
        } catch (SQLException ex) {
            Logger.getLogger(ManagerCategoria.class.getName()).log(Level.SEVERE,
                    "Error al buscar las cintas", ex);
        } catch (Exception ex) {
            Logger.getLogger(ManagerCategoria.class.getName()).log(Level.WARNING,
                    "Exepcion desconosida", ex);
        } finally {
            if (!externalCnn) {
                closeConexion();
            }
        }
        return list;
    }

    @Override
    public int add(Cinta o) throws SQLException {
        String query = "INSERT INTO c_cinta (colc_cinta) VALUES(?);";
        Prepare parametros = new Prepare(1);
        parametros.add(o.getColor(), Types.VARCHAR);
        int result = this.edit(query, parametros);
        int identificador = this.getLastIdInsert();
        o.setIdentificador(identificador);
        if(!externalCnn){
            this.closeConexion();
        }
        return result;
    }

    private synchronized Cinta makeObject(ResultSet r) throws SQLException {
        int identificador = r.getInt("idc_cinta");
        String color = r.getString("colc_cinta");
        return new Cinta(identificador, color);
    }

    @Override
    public List<Cinta> getAll() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int update(Cinta o, Cinta n) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int remove(Cinta o) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
